#ifndef ___pika_platform_thread__H
#define ___pika_platform_thread__H
#include "PikaPlatform.h"
#include "PikaObj.h"
#include <time.h>
#if defined(__linux) || (PIKA_WIN_PTHREAD_ENABLE)
#include <pthread.h>
typedef pthread_mutex_t pika_mutex_platform_data_t;
#elif PIKA_FREERTOS_ENABLE
#include "FreeRTOS.h"
#include "semphr.h"
#include "event_groups.h"
typedef SemaphoreHandle_t pika_mutex_platform_data_t;
#define MAX_WAITING_THREADS 1000
#elif PIKA_RTTHREAD_ENABLE
#include <rtthread.h>
typedef rt_mutex_t pika_mutex_platform_data_t;
#elif PIKA_ZEUSOS_ENABLE
#include "zos.h"
typedef zos_mutex_t pika_mutex_platform_data_t;
#else
typedef void* pika_mutex_platform_data_t;
#endif
#include <errno.h>


//-------------互斥锁------------------
typedef struct pika_platform_thread_lock {
    pika_mutex_platform_data_t mutex;
    volatile int is_init;
    volatile int is_first_lock;
    volatile int bare_lock;
} pika_platform_thread_lock_t;

int pika_platform_thread_lock_init(pika_platform_thread_lock_t* m);
int pika_platform_thread_lock_release(pika_platform_thread_lock_t* m);
int pika_platform_thread_lock_destroy(pika_platform_thread_lock_t* m);
int pika_platform_thread_lock_trylock(pika_platform_thread_lock_t* m);
int pika_platform_thread_lock_locked(pika_platform_thread_lock_t* m);
int pika_platform_thread_lock_acquire(pika_platform_thread_lock_t *m, pika_bool block, pika_float timeout);

//-------------条件变量------------------
typedef struct {
#if defined(__linux) || (PIKA_WIN_PTHREAD_ENABLE)
    pika_platform_thread_rlock_t rtmutex;
    pthread_cond_t cond;
    pthread_t owner;  // 当前持有锁的线程 ID
#elif PIKA_FREERTOS_ENABLE
    SemaphoreHandle_t mutex;  //可重入互斥锁
    SemaphoreHandle_t semaphore; //二值信号量
    volatile size_t waiting_threads;  // 记录等待线程的数量
#elif PIKA_RTTHREAD_ENABLE
#elif PIKA_ZEUSOS_ENABLE
#else
#endif
}pika_platform_thread_cond_t;


int pika_platform_thread_cond_init(pika_platform_thread_cond_t* cond);
void pika_platform_thread_cond_destory(pika_platform_thread_cond_t* cond);
int pika_platform_thread_cond_notify(pika_platform_thread_cond_t *cond, int num);
int pika_platform_thread_cond_notify_all(pika_platform_thread_cond_t *cond);
int pika_platform_thread_cond_wait(pika_platform_thread_cond_t *cond, pika_float timeout);
int pika_platform_thread_cond_acquire(pika_platform_thread_cond_t *cond, pika_bool block, pika_float timeout);
int pika_platform_thread_cond_release(pika_platform_thread_cond_t *cond);



//-------------可重入互斥锁------------------

// 可重入带超时互斥锁结构体
typedef struct {
#if defined(__linux) || (PIKA_WIN_PTHREAD_ENABLE)
    pthread_mutex_t mutex; //互斥锁变量(非指针)
    pthread_cond_t cond;
    pthread_t owner;  // 当前持有锁的线程 ID
    int count;        // 递归深度
#elif PIKA_FREERTOS_ENABLE
    SemaphoreHandle_t mutex; //信号量指针
    // TaskHandle_t owner; // 当前拥有锁的任务
    // int count;   // 锁的持有次数
#elif PIKA_RTTHREAD_ENABLE
#elif PIKA_ZEUSOS_ENABLE
#else
#endif
    
} pika_platform_thread_rlock_t;

// 初始化递归互斥锁
int pika_platform_thread_rlock_init(pika_platform_thread_rlock_t *rtm);

// 销毁递归互斥锁
void pika_platform_thread_rlock_destroy(pika_platform_thread_rlock_t *rtm);

// 带超时的递归互斥锁加锁
int pika_platform_thread_rlock_acquire(pika_platform_thread_rlock_t *rtm, pika_bool block, pika_float timeout);

// 递归互斥锁解锁
int pika_platform_thread_rlock_release(pika_platform_thread_rlock_t *rtm);
// 是否已获得锁
int pika_platform_thread_rlock_locked(pika_platform_thread_rlock_t *rtm);
// 是否本线程拥有锁
int pika_platform_thread_rlock_owned(pika_platform_thread_rlock_t *rtm);


//-------------------信号量------------------

// 递归带超时互斥锁结构体
typedef struct {
#if defined(__linux) || (PIKA_WIN_PTHREAD_ENABLE)
#elif PIKA_FREERTOS_ENABLE
    SemaphoreHandle_t semaphore;
    // int max;   // 
#elif PIKA_RTTHREAD_ENABLE
#elif PIKA_ZEUSOS_ENABLE
#else
#endif
    
} pika_platform_thread_semaphore_t;

int pika_platform_thread_semaphore_counting_init(pika_platform_thread_semaphore_t *sem, int maxcnt, int value);
int pika_platform_thread_semaphore_mutex_init(pika_platform_thread_semaphore_t *sem);
int pika_platform_thread_semaphore_binary_init(pika_platform_thread_semaphore_t *sem);
int pika_platform_thread_semaphore_acquire(pika_platform_thread_semaphore_t *sem, pika_bool block, pika_float timeout);
int pika_platform_thread_semaphore_release(pika_platform_thread_semaphore_t *sem);
void pika_platform_thread_semaphore_destroy(pika_platform_thread_semaphore_t *sem);

//-------------------------------------


#endif // end of file